package com.example.ass7.dataloader;

import java.util.HashMap;
import java.util.Map;

import com.example.ass7.sprite.Block;

/**
 * @author Meirav Ben Izhak
 */
public class BlocksFromSymbolsFactory {

    private final Map<String, Integer> spacerWidths;
    private final Map<String, BlockCreator> blockCreators;

    /**
     * constructor.
     */
    public BlocksFromSymbolsFactory() {
        this.spacerWidths = new HashMap<String, Integer>();
        this.blockCreators = new HashMap<String, BlockCreator>();
    }

    // returns true if 's' is a valid space symbol.
    /**
     * @param s
     *            s
     * @return boolean
     */
    public boolean isSpaceSymbol(String s) {
        Integer width = this.spacerWidths.get(s);
        return width != null;
    }

    // returns true if 's' is a valid block symbol.
    /**
     * @param s
     *            s
     * @return boolean
     */
    public boolean isBlockSymbol(String s) {
        BlockCreator blockCreator = this.blockCreators.get(s);
        return blockCreator != null;
    }

    // Return a block according to the definitions associated
    // with symbol s. The block will be located at position (xpos, ypos).
    /**
     * @param s
     *            s
     * @param x
     *            x
     * @param y
     *            y
     * @return Block
     */
    public Block getBlock(String s,
            int x,
            int y) {
        return this.blockCreators.get(s).create(x,
                y);
    }

    // Returns the width in pixels associated with the given spacer-symbol.
    /**
     * @param s
     *            s
     * @return int
     */
    public int getSpaceWidth(String s) {
        return this.spacerWidths.get(s);
    }

    /**
     * @param symbol
     *            symbol
     * @param blockCreator
     *            blockCreator
     */
    public void addBlockCreators(String symbol,
            BlockCreator blockCreator) {
        this.blockCreators.put(symbol,
                blockCreator);
    }

    /**
     * @param symbol
     *            symbol
     * @param width
     *            width
     */
    public void addSpacerWith(String symbol,
            Integer width) {
        this.spacerWidths.put(symbol,
                width);
    }

}